$(function() {
    var left = $("#left_pane");
    var number = [0, 0, 0];
    var nstr;
    var cstr;
    $(":header").each(function() {
        var $this = $(this);
        var item = $("<div></div>");
        if ($this.is("h1")) {
            item.addClass("m1");
            number[0]++;
            number[1] = 0;
            nstr = "" + number[0];
            cstr = "n1";
        } else if ($this.is("h2")) {
            item.addClass("m2");
            number[1]++;
            number[2] = 0;
            nstr = "" + number[0] + "." + number[1];
            cstr = "n2";
        } else if ($this.is("h3")) {
            item.addClass("m3");
            number[2]++;
            nstr = "" + number[0] + "." + number[1] + "." + number[2];
            cstr = "n3";
        } else {
            return;
        }

        var itemText = $this.text();
        var nspan = "<span class='" + cstr + "'>" + nstr + "</span>";
        $this.prepend(nspan);
        $("<a name='h" + nstr + "'></a>").insertBefore($this);

        item.append(nspan)
            .append($("<a></a>").attr("href", "#h" + nstr).text(itemText));
        item.appendTo(left);
    });
    $("#right_pane a[href ^= http]").each(function() {
        $(this).attr("target", "_blank");
    });
});
